﻿using System;
using System.Drawing;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.KDT.GC;
using Company.BLL.DuLieuChuan;
using Company.Interface.GC;
using Company.Interface.SXXK;
using Janus.Windows.GridEX;
using Janus.Windows.GridEX.EditControls;

namespace Company.Interface.KDT.GC
{
    public partial class HangChuyenTiepForm : BaseForm
    {
        //-----------------------------------------------------------------------------------------
        public HangChuyenTiepCollection HCTCollection = new HangChuyenTiepCollection();
        public Company.BLL.GC.HopDong HD = new Company.BLL.GC.HopDong();
        private bool IsExit = true;
        public string MaLoaiHinh = string.Empty;
        
        public string MaNguyenTe;
        
        //-----------------------------------------------------------------------------------------

        public HangChuyenTiepForm()
        {
            InitializeComponent();
        }
        
        private void khoitao_DuLieuChuan()
        {
            // Nước XX.
            this._Nuoc = Nuoc.SelectAll();

            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.SelectedValue = GlobalSettings.DVT_MAC_DINH;
        }
        

        //-----------------------------------------------------------------------------------------
        private bool checkMaHangExit(string maHang)
        {
            foreach (HangChuyenTiep hct in this.HCTCollection)
            {
                if (hct.MaHang.Trim() == maHang) return true;
            }
            return false;
        }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (checkMaHangExit(txtMaHang.Text)) 
            {
                ShowMessage("Mặt hàng này đã có rồi, bạn hãy chọn mặt hàng khác.", false);
                return;
            }
            cvError.Validate();
            if (!cvError.IsValid) return;
            if (!IsExit) return;
            HangChuyenTiep hct = new HangChuyenTiep();
            hct.SoThuTuHang = 0;
            hct.MaHS = txtMaHS.Text;
            hct.MaHang = txtMaHang.Text;
            hct.TenHang = txtTenHang.Text;
            hct.ID_NuocXX = ctrNuocXX.Ma;
            hct.ID_DVT = cbDonViTinh.SelectedValue.ToString();
            hct.SoLuong = Convert.ToDecimal(txtLuong.Value);
            hct.DonGia = Convert.ToDecimal(txtDGNT.Value);
            hct.TriGia = Convert.ToDecimal(txtTGNT.Value);
            this.HCTCollection.Add(hct);
            this.refresh_STTHang();
            this.Clear();
            dgList.DataSource = this.HCTCollection;
            dgList.Refetch();
        }

        private void Clear()
        {
            txtMaHang.Text = "";
            txtTenHang.Text = "";
            txtMaHS.Text = "";
        }

        //-----------------------------------------------------------------------------------------
        private void txtMaNPL_Leave(object sender, EventArgs e)
        {
            //this.fillNguyenPhuLieu(txtMaNPL.Text);
        }

        
        //-----------------------------------------------------------------------------------------------
        private void refresh_STTHang()
        {
            int i = 1;
            foreach (HangChuyenTiep hct in this.HCTCollection)
            {
                hct.SoThuTuHang = i++;
            }
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells["ID_NuocXX"].Text = this.Nuoc_GetName(e.Row.Cells["ID_NuocXX"].Value.ToString());
            e.Row.Cells["ID_DVT"].Text = this.DonViTinh_GetName(e.Row.Cells["ID_DVT"].Text.ToString());
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaHang_ButtonClick(object sender, EventArgs e)
        {

            switch (this.MaLoaiHinh.Substring(2,2))
            {
                case "PL":
                    Company.BLL.GC.NguyenPhuLieu npl = new Company.BLL.GC.NguyenPhuLieu();
                    npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    npl.MaHaiQuan = HD.MaHaiQuan;
                    npl.NgayKy = HD.NgayKy;
                    npl.SoHopDong = HD.SoHopDong;
                    Company.Interface.GC.NguyenPhuLieuRegistedGCForm fNPL = new Company.Interface.GC.NguyenPhuLieuRegistedGCForm();
                    fNPL.NguyenPhuLieuSelected = npl;
                    fNPL.ShowDialog();
                    if (fNPL.NguyenPhuLieuSelected.Ma != "")
                    {
                        txtMaHang.Text = npl.Ma;
                        txtTenHang.Text = npl.Ten;
                        txtMaHS.Text = npl.MaHS;
                        cbDonViTinh.SelectedValue = npl.DVT_ID;
                    }
                    break;
                case "SP":
                    Company.BLL.GC.SanPham sp = new Company.BLL.GC.SanPham();
                    sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    sp.MaHaiQuan = HD.MaHaiQuan;
                    sp.NgayKy = HD.NgayKy;
                    sp.SoHopDong = HD.SoHopDong;
                    Company.Interface.GC.SanPhamRegistedGCForm fsp = new Company.Interface.GC.SanPhamRegistedGCForm();
                    fsp.SanPhamSelected = sp;
                    fsp.ShowDialog();
                    if (fsp.SanPhamSelected.Ma != "")
                    {
                        txtMaHang.Text = sp.Ma;
                        txtTenHang.Text = sp.Ten;
                        txtMaHS.Text = sp.MaHS;
                        cbDonViTinh.SelectedValue = sp.DVT_ID;
                    }
                    break;
                case "TB":
                    Company.BLL.GC.ThietBi tb = new Company.BLL.GC.ThietBi();
                    tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    tb.MaHaiQuan = HD.MaHaiQuan;
                    tb.NgayKy = HD.NgayKy;
                    tb.SoHopDong = HD.SoHopDong;
                    Company.Interface.GC.ThietBiRegistedForm ftb = new Company.Interface.GC.ThietBiRegistedForm();
                    ftb.ThietBiSelected = tb;
                    ftb.ShowDialog();
                    if (ftb.ThietBiSelected.Ma!="")
                    {
                        txtMaHang.Text = tb.Ma;
                        txtTenHang.Text = tb.Ten;
                        txtMaHS.Text = tb.MaHS;
                        cbDonViTinh.SelectedValue = tb.DVT_ID;
                    }
                    break;
            }
        }

        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                HangChuyenTiepEditForm f = new HangChuyenTiepEditForm();
                f.HCT = (HangChuyenTiep)e.Row.DataRow;
                int i = this.getPostion(f.HCT);
                f.HCTCollection = this.HCTCollection;
                f.HCTCollection.RemoveAt(i);
                f.HD = this.HD;
                f.MaLoaiHinh = this.MaLoaiHinh;
                f.MaHaiQuan = this.MaHaiQuan;
                f.MaNguyenTe = this.MaNguyenTe;
                f.ShowDialog();
                if (f.IsEdited)
                {
                    if (f.HCT.Master_ID > 0)
                        f.HCT.Update();
                    else
                        this.HCTCollection.Insert(i, f.HCT);
                }
                else if (f.IsDeleted)
                {
                    if (f.HCT.Master_ID > 0)
                        f.HCT.Delete();
                    //this.TKCT.HCTCollection.RemoveAt(i.Position);
                }
                dgList.DataSource = this.HCTCollection;
                dgList.Refetch();
            }
        }
        private int getPostion(HangChuyenTiep hct)
        {
            for (int i = 0; i < this.HCTCollection.Count; i++)
            {
                if (this.HCTCollection[i].MaHang == hct.MaHang) return i;
            }
            return -1;
        }
        private void HangChuyenTiepForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            lblMaNT1.Text = lblMaNT2.Text = "(" + this.MaNguyenTe + ")";
            dgList.DataSource = this.HCTCollection;
        }

        private void txtLuong_Leave(object sender, EventArgs e)
        {
            txtTGNT.Value = (decimal)txtLuong.Value * (decimal)txtDGNT.Value;
        }

        private void txtDGNT_Leave(object sender, EventArgs e)
        {
            txtTGNT.Value = (decimal)txtLuong.Value * (decimal)txtDGNT.Value;
        }

        private void dgList_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa hàng hóa này không?", true) == "Yes")
                {
                    HangChuyenTiep hct = (HangChuyenTiep)e.Row.DataRow;
                    if (hct.ID > 0)
                    {
                        hct.Delete();
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void txtMaHang_Leave(object sender, EventArgs e)
        {
            switch (this.MaLoaiHinh.Substring(2, 2))
            {
                case "PL":
                    Company.BLL.GC.NguyenPhuLieu npl = new Company.BLL.GC.NguyenPhuLieu();
                    npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    npl.MaHaiQuan = HD.MaHaiQuan;
                    npl.NgayKy = HD.NgayKy;
                    npl.SoHopDong = HD.SoHopDong;
                    npl.Ma = txtMaHang.Text;
                    if (npl.Load())
                    {
                        this.IsExit = true;
                        txtMaHang.Text = npl.Ma;
                        txtTenHang.Text = npl.Ten;
                        txtMaHS.Text = npl.MaHS;
                        cbDonViTinh.SelectedValue = npl.DVT_ID;
                        epError.SetError(txtMaHang, string.Empty);
                    }
                    else
                    {
                        this.IsExit = false;
                        epError.SetIconPadding(txtMaHang, -8);
                        epError.SetError(txtMaHang, "Không tồn tại nguyên phụ liệu này.");

                        txtTenHang.Text = txtMaHS.Text = string.Empty;
                    }
                    break;
                case "SP":
                    Company.BLL.GC.SanPham sp = new Company.BLL.GC.SanPham();
                    sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    sp.MaHaiQuan = HD.MaHaiQuan;
                    sp.NgayKy = HD.NgayKy;
                    sp.SoHopDong = HD.SoHopDong;
                    sp.Ma = txtMaHang.Text;
                    if (sp.Load())
                    {
                        this.IsExit = true;
                        txtMaHang.Text = sp.Ma;
                        txtTenHang.Text = sp.Ten;
                        txtMaHS.Text = sp.MaHS;
                        cbDonViTinh.SelectedValue = sp.DVT_ID;
                        epError.SetError(txtMaHang, string.Empty);
                    }
                    else
                    {
                        this.IsExit = false;
                        epError.SetIconPadding(txtMaHang, -8);
                        epError.SetError(txtMaHang, "Không tồn tại sản phầm này.");

                        txtTenHang.Text = txtMaHS.Text = string.Empty;
                    }
                    break;
                case "TB":
                    Company.BLL.GC.ThietBi tb = new Company.BLL.GC.ThietBi();
                    tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                    tb.MaHaiQuan = HD.MaHaiQuan;
                    tb.NgayKy = HD.NgayKy;
                    tb.SoHopDong = HD.SoHopDong;
                    tb.Ma = txtMaHang.Text;
                    if (tb.Load())
                    {
                        this.IsExit = true;
                        txtMaHang.Text = tb.Ma;
                        txtTenHang.Text = tb.Ten;
                        txtMaHS.Text = tb.MaHS;
                        cbDonViTinh.SelectedValue = tb.DVT_ID;
                        epError.SetError(txtMaHang, string.Empty);
                    }
                    else
                    {
                        this.IsExit = false;
                        epError.SetIconPadding(txtMaHang, -8);
                        epError.SetError(txtMaHang, "Không tồn tại thiết bị này.");

                        txtTenHang.Text = txtMaHS.Text = string.Empty;
                    }
                    break;
            }
        }

    }
}